package com.xya.algorithm;

import com.xya.algorithm.bubble.BubbleSort;
import com.xya.algorithm.insertion.Insertion;
import com.xya.algorithm.selection.SelectionSort;

import java.util.Arrays;
import java.util.Random;

/**
 * 验证算法 - 选择排序 - 对数器
 */
public class DataChecker {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        boolean isRight = true;
        for (int i = 0; i < 1000; i++) {
            if (!check()) {
                isRight = false;
                break;
            }
        }
        long endTime = System.currentTimeMillis();

        System.out.println(isRight ? "right" : "wrong");
        System.out.println("共耗时" + (endTime - startTime) + "ms");
    }

    private static boolean check() {

        int[] arr1 = generateRandomArray();
        int[] arr2 = new int[arr1.length];
        //数组拷贝
        System.arraycopy(arr1, 0, arr2, 0, arr1.length);

        Arrays.sort(arr1);

        //选择排序
//        SelectionSort.sort(arr2);
//        SelectionSort.sort2(arr2);

        //冒泡排序
//        BubbleSort.sort(arr2);

        //插入排序
        Insertion.sort(arr2);

        boolean same = true;
        for (int i = 0; i < arr2.length; i++) {
            if (arr1[i] != arr2[i]) {
                same = false;
//                System.out.println("error: arr1["+i+"] = " + arr1[i] + ", arr2[" + i + "] = " + arr2[i]);
//                System.out.println("arr1 : " + Arrays.toString(arr1));
//                System.out.println("arr2 : " + Arrays.toString(arr2));
                break;
            }
        }
        return same;
    }

    private static int[] generateRandomArray() {
        Random r = new Random();
        int[] arr = new int[10000];
        for (int i = 0; i < arr.length; i++)
            arr[i] = r.nextInt(10000);
        return arr;
    }
}
